﻿using System;
using System.Net;

using Tulde.Core;
using System.IO.IsolatedStorage;
using System.IO;
using System.Xml.Serialization;

namespace Tulde.MP3tunes
{
    public class IsolatedStorageProvider : IStorageProvider
    {

        #region IStorageProvider Members

        public string Read(string path)
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {

                using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(path, System.IO.FileMode.OpenOrCreate, isf))
                {
                    using (StreamReader sr = new StreamReader(stream))
                    {
                        return sr.ReadToEnd();
                    }
                }
            }
        }
        public T Read<T>(string path)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            //FileStream file = new FileStream(strPath, FileMode.OpenOrCreate);

            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {

                using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(path, System.IO.FileMode.OpenOrCreate, isf))
                {
                    if (stream.Length > 0)
                    {

                        return (T)serializer.Deserialize(stream);
                    }
                    else
                    {
                        stream.Close();
                    }
                }
            }

            return default(T);
        }
        public T Read<T>(Type type, string path)
        {
            XmlSerializer serializer = new XmlSerializer(type);
            //FileStream file = new FileStream(strPath, FileMode.OpenOrCreate);

            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {

                using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(path, System.IO.FileMode.OpenOrCreate, isf))
                {
                    if (stream.Length > 0)
                    {

                        return (T)serializer.Deserialize(stream);
                    }
                    else
                    {
                        stream.Close();
                    }
                }
            }

            return default(T);
        }

        public void Load<T>(string path)
        {
            throw new NotImplementedException();
        }

        public void Delete(string path)
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                try
                {
                    isf.DeleteFile(path);
                }
                catch (Exception ex)
                {
                    // MessageBox.Show(ex.Message, "An error occured.", MessageBoxButton.OK);
                }

            }
        }
        public bool Exists(string path)
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {

                return isf.FileExists(path);

            }
        }

        #endregion

        #region IStorageProvider Members

        public void Save(string path, string data)
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {

                using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(path, System.IO.FileMode.OpenOrCreate, isf))
                {
                    using (StreamWriter sr = new StreamWriter(stream, System.Text.Encoding.UTF8))
                    {
                        sr.Write(data);
                        sr.Close();
                    }
                    stream.Close();
                }
            }
        }
        public void Save(Type type,object data, string path)
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                MemoryStream ms = new MemoryStream();
                XmlSerializer xs = new XmlSerializer(type);


                xs.Serialize(ms, data);

                ms.Seek(0, SeekOrigin.Begin);
                using (ms)
                {
                    IsolatedStorageFileStream file_stream = isf.CreateFile(path);
                    if (file_stream == null) throw new Exception();
                    int READ_CHUNK = 1024 * 1024;
                    int WRITE_CHUNK = 1024 * 1024;
                    byte[] buffer = new byte[READ_CHUNK];
                    while (true)
                    {
                        int read = ms.Read(buffer, 0, READ_CHUNK);
                        if (read <= 0) break;
                        int to_write = read;
                        while (to_write > 0)
                        {
                            file_stream.Write(buffer, 0, Math.Min(to_write, WRITE_CHUNK));
                            to_write -= Math.Min(to_write, WRITE_CHUNK);
                        }
                    }

                    file_stream.Close();
                }

            }
        }

        public void Save<T>(string path, T data)
        {
            Save(typeof(T), data, path);
            
        }
        public string[] ListAll()
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                return isf.GetFileNames();
            }
        }


        #endregion

        #region IStorageProvider Members


       
        #endregion
    }
}
